#pragma once

#include <string>

#include <pthread.h>

static int _s_num = 1;

class thread
{
    typedef void (*callback_t)(void);
    typedef thread self;
public:
    static void* Routine(void* args)
    {
        thread* tid = static_cast<thread*>(args);
        tid->entery();
        return nullptr;
    }
public:
    thread(callback_t cb)
        : _tid(0)
        , _name("")
        , _start_time(0)
        , _isrun(false)
        , _cb(cb)
    {}

    void run()
    {
        _name = "thread-" + std::to_string(_s_num++);
        _start_time = time(nullptr);
        _isrun = true;
        pthread_create(&_tid, nullptr, Routine, this);
    }

    void join()
    {
        pthread_join(_tid, nullptr);
        _isrun = false;
    }

    void detach()
    {
        pthread_detach(_tid);
    }

    void entery()
    {
        _cb();
    }
    bool isrunning() const
    {
        return _isrun;
    }
    std::string name() const
    {
        return _name;
    }
    uint64_t start_time() const
    {
        return _start_time;
    }

    ~thread()
    {}
private:
    pthread_t _tid;
    std::string _name;
    uint64_t _start_time;
    
    bool _isrun;
    callback_t _cb;
};